package com.bionet.keggservice.service.impl;

import cn.hutool.core.math.MathUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bionet.keggservice.entity.KeggCompoundLinkPathway;
import com.bionet.keggservice.mapper.KeggCompoundLinkPathwayMapper;
import com.bionet.keggservice.service.KeggCompoundLinkPathwayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hzx
 * @since 2021-06-28
 */
@Service
public class KeggCompoundLinkPathwayServiceImpl extends ServiceImpl<KeggCompoundLinkPathwayMapper, KeggCompoundLinkPathway> implements KeggCompoundLinkPathwayService {


    @Override
    public List<String> getCompoundInWhichPathway(String compound) {
        QueryWrapper<KeggCompoundLinkPathway> Wrapper = new QueryWrapper<>();
        Wrapper.select("target_db").like("source_db", compound);
        List<KeggCompoundLinkPathway> keggCompoundLinkPathways = baseMapper.selectList(Wrapper);
        ArrayList<String> singleList = new ArrayList<>();
        for (KeggCompoundLinkPathway cpd:keggCompoundLinkPathways){
            singleList.add(cpd.getTargetDb());
        }
        return singleList;
    }

    @Override
    public List<String> getCompoundInAPathway(String pathway) {
        QueryWrapper<KeggCompoundLinkPathway> Wrapper = new QueryWrapper<>();
        Wrapper.select("source_db").like("target_db", pathway);
        List<KeggCompoundLinkPathway> keggCompoundLinkPathways = baseMapper.selectList(Wrapper);
        ArrayList<String> singleList = new ArrayList<>();
        for (KeggCompoundLinkPathway cpd:keggCompoundLinkPathways){
            singleList.add(cpd.getSourceDb());
        }
        return singleList;
    }

    @Override
    public Integer countCompoundInAPathway(List<String> cpdList, String pathway) {
        List<String> compoundInAPathway = getCompoundInAPathway(pathway);
        Integer count = 0;
        for (String cpd : cpdList){
            if (compoundInAPathway.contains(cpd)){
                count++;
            }
        }
        return count;
    }

    @Override
    public Integer countCompoundInAllPathway(String endPathway) {
        QueryWrapper<KeggCompoundLinkPathway> Wrapper = new QueryWrapper<>();
        Wrapper.le("target_db", endPathway);
        List<KeggCompoundLinkPathway> keggCompoundLinkPathways = baseMapper.selectList(Wrapper);
        HashSet<String> totalCpds = new HashSet<>();
        for (KeggCompoundLinkPathway cpd:keggCompoundLinkPathways){
            totalCpds.add(cpd.getSourceDb());
        }
        int size = totalCpds.size();
        return size;
    }

    @Override
    public HashSet<String> getAllPathway(String endPathway) {
        QueryWrapper<KeggCompoundLinkPathway> Wrapper = new QueryWrapper<>();
        Wrapper.le("target_db", endPathway);
        List<KeggCompoundLinkPathway> keggCompoundLinkPathways = baseMapper.selectList(Wrapper);
        HashSet<String> totalPathWays = new HashSet<>();
        for (KeggCompoundLinkPathway cpd:keggCompoundLinkPathways){
            totalPathWays.add(cpd.getTargetDb());
        }
        return totalPathWays;
    }

    @Override
    public double Hypergeometric(Integer M, Integer N, Integer X, Integer K) {
        double TotalP = 0;
        for (Integer i = 0; i <= X; i++){
            BigDecimal a = new BigDecimal(MathUtil.combinationCount(K, i));
            System.out.println(a);
            BigDecimal b = new BigDecimal(MathUtil.combinationCount((M-K), (N-i)));
            System.out.println(b);
            BigDecimal c = new BigDecimal(MathUtil.combinationCount(M, N));
            System.out.println(c);
            TotalP += 0;
        }
        return TotalP;
    }
}
